package com.zahb.qadx.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.model.Org;
import com.zahb.qadx.ui.view.CircleImageView;
import com.zahb.qadx.util.ImageLoaderKt;

import java.util.List;

/**
 * Created by huwei.
 * Data 2020-10-12.
 * Time 15:29.
 */
public class EnterListAdapter extends RecyclerView.Adapter<EnterListAdapter.EnterViewHodler> {
    private Context mContext;
    private List<Org> moelList;
    int id;

    public EnterListAdapter(Context mContext, List<Org> moelList, int id) {
        this.mContext = mContext;
        this.moelList = moelList;
        this.id = id;
    }

    public int getSelectId(){
        return  id;
    }

    public interface onCheckBoxListener {
        void clickOk(boolean isChecked, int orgUserId, String rootOrgName);
    }

    private onCheckBoxListener mCheckboxListener;

    public void setOncheckListener(onCheckBoxListener mCheckboxListener) {
        this.mCheckboxListener = mCheckboxListener;
    }

    @NonNull
    @Override
    public EnterViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new EnterViewHodler(LayoutInflater.from(mContext).inflate(R.layout.item_enterprise, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull EnterViewHodler holder, int position) {
        holder.tv_title.setText(moelList.get(position).getRootOrgName());
        String orgLogo = moelList.get(position).getOrgLogo();
        List<Org> orgList = BaseApplication.getContext().getDataLogin().getOrgList();
        final int orgUserId = orgList.get(position).getOrgUserId();
        final int rootOrgId = orgList.get(position).getRootOrgId();
        final String rootOrgName = orgList.get(position).getRootOrgName();
        holder.img_pic.setBackgroundResource(R.drawable.shape_white_oval);
        ImageLoaderKt.loadImageCircle(holder.img_pic, orgLogo,R.mipmap.mine_icon);
        if (rootOrgId != id) {
            holder.checkBox.setSelected(false);
            holder.ll_bg.setBackground(mContext.getResources().getDrawable(R.color.white));
            holder.tv_title.setTextColor(mContext.getResources().getColor(R.color.black));
        } else {
            holder.checkBox.setSelected(true);
            holder.ll_bg.setBackground(mContext.getResources().getDrawable(R.color.blue_409eff));
            holder.tv_title.setTextColor(mContext.getResources().getColor(R.color.white));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCheckboxListener.clickOk(true, orgUserId, rootOrgName);
                id =rootOrgId;
            }
        });
    }

    @Override
    public int getItemCount() {
        return moelList != null ? moelList.size() : 0;
    }

    static class EnterViewHodler extends RecyclerView.ViewHolder {
        private TextView tv_title;
        private ImageView checkBox;
        private LinearLayout ll_bg;
        private CircleImageView img_pic;

        public EnterViewHodler(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_titile);
            checkBox = itemView.findViewById(R.id.cb_check);
            ll_bg = itemView.findViewById(R.id.ll_bg);
            img_pic = itemView.findViewById(R.id.img_pic);
        }
    }
}
